<template>
  <common-list>
    <!-- 头部搜素start -->
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        @submit.native.prevent
        :model="query"
        ref="queryForm"
        :inline="true"
        label-width="65px"
      >
        <el-form-item label="业务名称" prop="title">
          <common-input v-model="query.title" clearable @enter="handleQuery" />
        </el-form-item>

        <el-form-item label="操作类型" prop="businessType">
          <el-select v-model="query.businessType" filterable>
            <el-option label="全部" :value="null"></el-option>
            <el-option
              v-for="(item, index) in businessTypeList"
              :key="index"
              :label="item.name"
              :value="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="operName">
          <common-input
            v-model="query.operName"
            clearable
            @enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="源IP" prop="operIp">
          <common-input v-model="query.operIp" clearable @enter="handleQuery" />
        </el-form-item>

        <el-form-item label="操作日期" prop="daterange">
          <el-date-picker
            v-model="query.daterange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="query.status">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="成功" value="0"></el-option>
            <el-option label="失败" value="1"></el-option>
          </el-select>
        </el-form-item>
        <common-dept
          ref="commonDept"
          :currentParent="currentParent"
          @getDeptId="getDeptId"
          @getDeptList="getDeptList"
          @getLoading="getLoading"
        ></common-dept>
      </el-form>
    </common-search>
    <!-- 头部搜素end -->
    <!--列表 start-->
    <common-table slot="content" v-loading="loading" :data="list">
      <el-table-column label="业务名称" align="left" prop="title">
        <template slot-scope="{ row }">{{ row.title }}</template>
      </el-table-column>

      <el-table-column
        label="操作类型"
        align="left"
        prop="businessType"
      ></el-table-column>

      <el-table-column
        label="用户名"
        align="left"
        prop="operName"
        show-overflow-tooltip
      ></el-table-column>

      <el-table-column label="源IP" align="left" prop="operIp">
        <template slot-scope="{ row }">{{ row.operIp }}</template>
      </el-table-column>

      <el-table-column
        label="组织"
        align="left"
        prop="deptName"
        width="180"
        show-overflow-tooltip
      />

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <el-tag type="success" v-if="row.status === 0">成功</el-tag>
          <el-tag type="danger" v-else>失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="操作日期"
        align="left"
        prop="operTime"
        width="170"
      >
        <template slot-scope="{ row }">{{ row.operTime }}</template>
      </el-table-column>

      <el-table-column label="操作" align="left" width="52">
        <template slot-scope="{ row }">
          <el-button type="text" @click="detail(row)">详情</el-button>
        </template>
      </el-table-column>
    </common-table>
    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="query.pageNum"
      :limit.sync="query.pageSize"
      @pagination="getList"
    />

    <logDetail ref="logDetail" />
  </common-list>
</template>

<script>
import logDetail from "./components/logDetail";
import { operlog_list, getBussinessType } from "@/api/management/logs/logs.js";
import BaseList from "@/mixins/BaseList";
import { getSingleArray, getFullDept } from "@/utils/util";

export default {
  mixins: [BaseList],
  components: { logDetail },
  props: {},
  data() {
    return {
      filterMenu: "", //菜单筛选
      showSearch: true, // 查询部分显示
      total: 1, // 总条数
      loading: false, // table 加载中
      list: [], // 数据列
      query: {
        pageNum: 1,
        pageSize: 10,
        businessType: null,
        status: null,
        deptIds: null
      }, // 查询条件
      businessTypeList: [],
      currentParent: 1,
      singleDeptList: []
    };
  },
  watch: {},
  computed: {},
  methods: {
    getLoading() {
      this.loading = true;
    },
    getDeptList(list) {
      this.singleDeptList = getSingleArray(list);
      this.getList();
    },
    getDeptId(val) {
      this.query.deptIds = val;
    },
    getDeptName(val) {
      let dept = this.singleDeptList.find(item => item.id === val.toString());
      if (dept) return getFullDept(dept.fulllabel);
    },

    getBussinessType() {
      getBussinessType().then(res => {
        if (res && res.code === 200) {
          this.businessTypeList = res.data;
        } else {
          this.businessTypeList = [];
        }
      });
    },
    /**
     * 获取列表
     */
    getList() {
      this.loading = true;
      operlog_list({ ...this.query })
        .then(res => {
          if (res && res.code === 200) {
            this.list = res.rows.reduce((r, c) => {
              let bt = this.businessTypeList.find(
                item => item.code === c.businessType
              );
              r.push({
                ...c,
                businessType: bt ? bt.name : "其他",
                deptName: this.getDeptName(c.deptId || "")
              });

              return r;
            }, []);
            this.total = res.total;
          } else {
            this.list = [];
            this.total = 0;
            this.handleListTips(res);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * 查询
     */
    handleQuery() {
      if (!!this.query.daterange) {
        this.query.beginTime = this.query.daterange[0];
        this.query.endTime = this.query.daterange[1];
      } else {
        this.query.beginTime = "";
        this.query.endTime = "";
      }
      this.query.pageNum = 1;
      this.getList();
    },
    /**
     * 重置查询
     */
    resetQuery() {
      this.query = this.$options.data().query;
      this.$refs.commonDept.clearDept();
    },
    /**
     * 详情
     */
    detail(row) {
      this.$refs.logDetail.show(row ? { ...row } : {});
    }
  },
  created() {
    // this.getList();
    this.getBussinessType();
  },
  mounted() {}
};
</script>
